package nl.rainbowsheep.reisorg.rest.service.controller;

import java.util.Date;
import java.util.List;
import nl.rainbowsheep.reisorg.rest.service.controller.exception.ForbiddenException;
import nl.rainbowsheep.reisorg.rest.service.model.Customer;
import nl.rainbowsheep.reisorg.rest.service.model.Trip;
import nl.rainbowsheep.reisorg.rest.service.model.TripBooking;
import nl.rainbowsheep.reisorg.rest.service.service.CustomerService;
import nl.rainbowsheep.reisorg.rest.service.service.TripBookingService;
import nl.rainbowsheep.reisorg.rest.service.service.TripService;
import nl.rainbowsheep.reisorg.rest.service.viewmodel.TripBookingViewmodel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/trip/booking")
public class TripBookingController {

    @Autowired
    private TripService tripService;
    @Autowired
    private CustomerService customerService;
    @Autowired
    private TripBookingService tripBookingService;

    @RequestMapping(value = "/all", method = RequestMethod.GET)
    @ResponseBody
    public List<Trip> getAllBookingsByUsername(@RequestParam(value = "username", required = true) String username) {
        Customer c = customerService.getCustomerByUsername(username);
        if (c != null) {
            return tripService.findByUsernameId(c.getId());
        }
        throw new ForbiddenException();
    }

    @RequestMapping(value = "/allbookingviewmodels", method = RequestMethod.GET)
    @ResponseBody
    public List<TripBookingViewmodel> getAllBookingViewmodelsByCustomerId(@RequestParam(value = "customerid", required = true) long id) {
        return tripBookingService.findBookings(id);

    }

    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @ResponseBody
    public TripBooking saveTrip(@RequestBody TripBooking tripbooking) {
        tripbooking.setRegistrationDate(new Date());
        return tripBookingService.save(tripbooking);
    }
    
    @RequestMapping(value = "/getallbyusername", method = RequestMethod.GET)
    @ResponseBody
    public List<TripBookingViewmodel> getAllBookingViewmodelsByUsername(@RequestParam(value = "username", required = true) String username) {
        long userid = customerService.getCustomerByUsername(username).getId();
        return tripBookingService.findBookings(userid);
    }
}
